<template>
  <view class="container">
    <scroll-view>
      <uni-list>
        <uni-list-item
            v-for="(item,index) in tableData" :title="item.name"
            :key="index" :note="'申领合计：'+item.total" show-extra-icon
            showArrow :extra-icon="{color: '#4cd964',size: '22',type: 'person'}"
            thumb-size="lg" clickable  @click="onClick(item)"></uni-list-item>
      </uni-list>
    </scroll-view>
    <view style="display: flex;justify-content: center;">
      <button class="button" type="primary" @click="submit">结束</button>
    </view>
  </view>
</template>

<script>
import userInfoMixin from "../../common/js/userInfoMixin";
import {
  mapMutations
} from 'vuex'

export default {
  mixins: [userInfoMixin],
  data() {
    return {
      name: '101',
      tableData: [],
      openid: '',
      nickName: '',
      sum: 0,
    }
  },
  methods: {
    ...mapMutations(['setCurrentSyr']),
    submit() {
      uni.navigateTo({
        url: `/pages/400end/400end`,
        success() {
          console.log('jump succ')
        },
        fail() {
          console.log('jump failed')
        }
      })
    },

    onClick(item){
      this.setCurrentSyr({
        ...item
      })
      uni.navigateTo({
        url: `/pages/300fpjqr_detail/300fpjqr`,
        success() {
          console.log('jump succ')
        },
        fail() {
          console.log('jump failed')
        }
      })
    },
    getTableData() {
      let db = wx.cloud.database(), that = this
      db.collection('JCRS')
          .orderBy('index', 'asc')
          .where({
            openid: that.currentUser.openid,
          })
          .get()
          .then(res => {
            that.range = res.data.map(v => {
              return {
                value: v.name,
                text: v.name
              }
            })
            that.sum = 0
            console.log('原始数据：', res)
            that.tableData = res.data.map(v => {
              let item = {
                name:v.name,
                dqsl: v.dqsl && v.dqsl.total || 0,
                rxjlj_bk: (v.rxjlj && v.rxjlj.eduBackground1 ? parseFloat(v.rxjlj.amount1) : 0) || 0,
                rxjlj_ss: (v.rxjlj && v.rxjlj.eduBackground2 ? parseFloat(v.rxjlj.amount2) : 0) || 0,
                rxjlj_bs: (v.rxjlj && v.rxjlj.eduBackground3 ? parseFloat(v.rxjlj.amount3) : 0) || 0,
                xfzcj_bk: (v.xfzcj && v.xfzcj.eduBackground1 ? parseFloat(v.xfzcj.amount1) : 0) || 0,
                xfzcj_ss: (v.xfzcj && v.xfzcj.eduBackground2 ? parseFloat(v.xfzcj.amount2) : 0) || 0,
                xfzcj_bs: (v.xfzcj && v.xfzcj.eduBackground3 ? parseFloat(v.xfzcj.amount3) : 0) || 0,
                byjlj_bk: (v.byjlj && v.byjlj.eduBackground1 ? parseFloat(v.byjlj.amount1) : 0) || 0,
                byjlj_ss: (v.byjlj && v.byjlj.eduBackground2 ? parseFloat(v.byjlj.amount2) : 0) || 0,
                byjlj_bs: (v.byjlj && v.byjlj.eduBackground3 ? parseFloat(v.byjlj.amount3) : 0) || 0,
                jhzfj: v.jhzfj ? parseFloat(v.jhzfj.amount) : 0,
                syzfj: v.syzfj ? parseFloat(v.syzfj.amount) * v.syzfj.times : 0,
                hyhxjlj: v.hyhxjlj ? parseFloat(v.hyhxjlj.amount) : 0,
                hbshfzj: v.hbshfzj ? parseFloat(v.hbshfzj.amount) : 0,
                zyfzzcj: v.zyfzzcj ? parseFloat(v.zyfzzcj.amount) : 0,
                jntszcj: v.jntszcj ? parseFloat(v.jntszcj.amount) * v.jntszcj.times : 0,
                cyzcj: v.cyzcj ? parseFloat(v.cyzcj.amount) : 0,
                tbzcj: v.tbzcj ? parseFloat(v.tbzcj.amount) * v.tbzcj.times : 0,
                gfxfzcj: v.gfxfzcj ? parseFloat(v.gfxfzcj.amount) : 0,
                lyzcj: v.lyzcj ? parseFloat(v.lyzcj.amount) * v.lyzcj.times : 0,
                dqshf1: v.dqshf && v.dqshf.type1.length ? parseFloat(v.dqshf.amount1) * v.dqshf.times1 : 0,
                dqshf2: v.dqshf && v.dqshf.type2.length ? parseFloat(v.dqshf.amount2) * v.dqshf.times2 : 0,
                zdjbjzj: v.zdjbjzj && v.zdjbjzj.type1 ? 10 : 0,// 这个地方固定一下，有需求的话再修改
                jcsfybj: v.jcsfybj && parseFloat(v.jcsfybj.amount) || 0,
                fcsybj: v.fcsybj && parseFloat(v.fcsybj.amount) || 0,
                jdsl1: v.jdsl && v.jdsl.amount1 ? parseFloat(v.jdsl.amount1) : 0,
                jdsl2: v.jdsl && v.jdsl.amount2 ? parseFloat(v.jdsl.amount2) : 0,
                jdsl3: v.jdsl && v.jdsl.amount3 ? parseFloat(v.jdsl.amount3) : 0,
                jdsl4: v.jdsl && v.jdsl.amount4 ? parseFloat(v.jdsl.amount4) : 0
              }
              item.total = Object.keys(item)
                  .filter(v => !['jdsl1', 'jdsl2', 'jdsl3', 'jdsl4', 'name'].includes(v))
                  .reduce((ii, jj) => ii + item[jj], 0)
              console.log('item.total: ', item.total)
              that.sum += item.total //最後顯示的總數
              return item
            })
            console.log('确认之后返回：', res)
          })
    },
  },
  mounted() {
    this.getTableData()
  }
}
</script>

<style>

</style>
